<template>
  <page-header-wrapper :title="false">
    <a-card title="未成单客户列表">
      <div class="table-page-search-wrapper">
        <a-form-model :model="queryParam" layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="24">
              <a-form-model-item label="跨库资源">
                <a-select v-model="queryParam['filter[is_hot]']" placeholder="请选择是否筛选热资源">
                  <a-select-option v-for="(item,index) in hotOpts" :key="index" :value="index">{{ item }}</a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="业务名称">
                <a-input v-model="queryParam['filter[tm_name]']" placeholder="输入业务名称"/>
              </a-form-model-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="6" :sm="24">
                <a-form-model-item label="客户姓名">
                  <a-input v-model="queryParam['filter[customer_name]']" placeholder="输入客户姓名"/>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="联系电话">
                  <a-input v-model="queryParam['filter[phone]']" placeholder="输入联系电话"/>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="领取时间">
                  <a-range-picker v-model="queryParam.received_at" @change="onSubmitTime" />
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="标记类型">
                  <a-select v-model="queryParam['filter[customer_status]']" placeholder="请选择标记类型">
                    <a-select-option v-for="(item,index) in statusOpts" :key="index" :value="index">{{ item }}</a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
            </template>
            <a-col :md="!advanced && 8 || 24" :sm="24">
              <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="reset">重置</a-button>
                <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'"/>
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
      <div class="contract_content">
        <div :style="{ marginBottom: '20px' }">
          <a-button :disabled="selectedRowKeys.length === 0" size="small" type="danger" @click="batchAbandon">批量放弃</a-button>
        </div>
        <s-table
          ref="table"
          bordered
          :row-selection="rowSelection"
          :row-key="(record) => record.customer_id"
          :columns="customerColumns"
          :data="unpaidData"
        >
          <div
            slot="filterDropdown"
            slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
            style="padding: 8px"
          >
            <a-input
              v-ant-ref="c => (searchInput = c)"
              :placeholder="`搜索${column.title}`"
              :value="selectedKeys[0]"
              style="width: 188px; margin-bottom: 8px; display: block;"
              @change="e => setSelectedKeys(e.target.value ? [e.target.value] : [])"
              @pressEnter="() => handleSearch(selectedKeys, confirm, column.dataIndex)"
            />
            <a-button
              type="primary"
              icon="search"
              size="small"
              style="width: 90px; margin-right: 8px"
              @click="() => handleSearch(selectedKeys, confirm, column.dataIndex)"
            >
              搜索
            </a-button>
            <a-button size="small" style="width: 90px" @click="() => handleReset(clearFilters)">
              重置
            </a-button>
          </div>
          <template slot="tm_name" slot-scope="text, record">
            {{ text }}{{ record.pt_name }}
          </template>
          <template slot="phone" slot-scope="text, record">
            {{ text }} <a-icon type="phone" theme="filled" :style="record.is_hot == 0 ? { color: '#1890ff', cursor: 'pointer' } : { color: '#ff0000', cursor: 'pointer' }" @click="makePhone(record.customer_id, record.is_hot)" />
          </template>
          <template slot="action" slot-scope="text, record">
            <a-button size="small" type="primary" style="margin-right:5px;" @click="$router.push({ name: 'CustomerDetail', params: {id: record.customer_id} })">查看</a-button>
            <a-button :disabled="record.is_hot == 1" size="small" type="danger" style="margin-right:5px;" @click="abandon(record.customer_id)">放弃</a-button>
            <a-button size="small" type="primary" @click="$router.push({ name: 'AddContract', query: {customer_id: record.customer_id} })">做合同</a-button>
          </template>
        </s-table>
      </div>
    </a-card>
    <phone-modal v-if="modalVisible" :customer-id="customerId" @cancel="modalExit"></phone-modal>
    <phone-hot v-if="hotVisible" :customer-id="customerId" @cancel="hotExit"></phone-hot>
    <phone-iso v-if="isoVisible" :customer-id="customerId" @cancel="isoExit"></phone-iso>
  </page-header-wrapper>
</template>

<script>
  import { STable, PhoneModal, PhoneHot, PhoneIso } from '@/components'
  import { batchAbandon, takeAbandon, unpaid } from '@/api/customer'
  import col from '@/columns/customer'
  const customerColumns = col.unpay

  export default {
    components: {
      PhoneIso,
      STable,
      PhoneModal,
      PhoneHot
    },
    name: 'UnpayCustomer',
    data () {
      return {
        customerColumns,
        unpaidData: parameters => {
          return unpaid(Object.assign(parameters, this.queryParam)).then(res => {
            this.hotOpts = res.result.hot_opts
            this.statusOpts = res.result.customer_status_opts
            return res.result
          })
        },
        queryParam: {},
        advanced: false,
        selectedRowKeys: [],
        modalVisible: false,
        hotVisible: false,
        isoVisible: false,
        customerId: '',
        visible: false,
        hotOpts: {},
        statusOpts: {}
      }
    },
    activated () {
      this.$refs.table.refresh()
    },
    methods: {
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      makePhone (id, hot) {
        this.customerId = id
        if (hot === '0') {
          this.modalVisible = true
        } else {
          this.hotVisible = true
        }
      },
      modalExit (visible) {
        this.modalVisible = visible
      },
      hotExit (visible) {
        this.hotVisible = visible
      },
      isoExit (visible) {
        this.isoVisible = visible
      },
      handleSearch (selectedKeys, confirm, dataIndex) {
        confirm()
        this.searchText = selectedKeys[0]
        this.searchedColumn = dataIndex
      },
      handleReset (clearFilters) {
        clearFilters()
      },
      onSelectChange (selectedRowKeys) {
        this.selectedRowKeys = selectedRowKeys
      },
      onSubmitTime (date, dateString) {
        for (let i = 0; i < dateString.length; i++) {
          if (i === 0) {
            this.queryParam['filter[received_at_start]'] = dateString[i]
          }
          if (i === 1) {
            this.queryParam['filter[received_at_end]'] = dateString[i]
          }
        }
      },
      abandon (id) {
        const app = this
        app.$confirm({
          title: '警告',
          content: '是否确定休了客户',
          onOk () {
            takeAbandon(id).then(res => {
              if (res.status === 0) {
                app.$refs.table.refresh()
              } else {
                app.$warning({
                  title: res.msg
                })
              }
            })
          }
        })
      },
      batchAbandon () {
        const app = this
        app.$confirm({
          title: '警告',
          content: '是否放弃' + app.selectedRowKeys.length + '条客户资源',
          onOk () {
            const parameter = {}
            parameter['customer_ids'] = app.selectedRowKeys
            batchAbandon(parameter).then(res => {
              if (res.status === 0) {
                app.$refs.table.refresh()
                app.selectedRowKeys = []
              } else {
                app.$warning({
                  title: res.msg
                })
              }
            })
          }
        })
      },
      addModal () {
        this.visible = true
      },
      reset () {
        this.queryParam = {}
        this.$refs.table.refresh(true)
      }
    },
    computed: {
      rowSelection () {
        return {
          onChange: this.onSelectChange,
          getCheckboxProps: record => ({
            props: {
              disabled: record.is_hot === '1', // Column configuration not to be checked
              name: record.name
            }
          })
        }
      }
    }
  }
</script>

<style scoped>

</style>
